<script setup lang="ts">
import type { FormInstance } from 'element-plus'
import { defaultFormData, formRules } from '../index'
import type { CustomDialogInstance } from '@/types'
import { sysDepartmentalApi } from '@/api'
import { ENERGY } from '@/constant'
// ------------------use------------------
// #region
defineProps<{
  title: string
}>()
const emits = defineEmits<{
  'save-success': []
}>()
const formData = defineModel('data', {
  default: () => defaultFormData,
})
const { withLoading, vLoading } = useDefineLoading({
  destroyNode: true,
})
const [departmentData, setDepartmentData] = useState<any[]>(
  [],
)
// #endregion
// ------------------静态变量------------------
// #region

// #endregion
// ------------------动态变量------------------
// #region

// #endregion
// ------------------ref-----------
// #region
const dialogRef = ref<CustomDialogInstance | null>(null)
const formRef = ref<FormInstance | null>(null)
// #endregion
// ------------------reactive------
// #region
// #endregion
// ------------------computed------
// #region

// #endregion
// ------------------生命周期------------------
// #region

// #endregion
// ------------------内置方法---------------------
// #region
// #endregion
// ------------------方法---------------------
// #region
async function getDepartmentData() {
  if (departmentData.value.length > 0)
    return
  const { data: res } = await withLoading(
    sysDepartmentalApi.queryTreeList,
  )()
  setDepartmentData(res)
}
async function open() {
  dialogRef.value?.open?.()
  await nextTick()
  dialogRef.value?.setSaving?.(false)
  formRef.value?.resetFields()
  getDepartmentData()
}
function close() {
  dialogRef.value?.close?.()
}
async function clickSave(done: () => void) {
  try {
    await formRef.value?.validate()
    emits('save-success')
  }
  catch (error) {
    done()
  }
}
// #endregion
defineExpose({
  open,
  close,
})
</script>

<template>
  <CustomDialog
    ref="dialogRef"
    :title="title"
    width="600px"
    @confirm="clickSave"
  >
    <el-form
      ref="formRef"
      v-loading
      label-width="120px"
      label-position="right"
      :model="formData"
      :rules="formRules"
      inline
    >
      <el-form-item label="所属部门" class="width-fill">
        <el-cascader
          v-model="formData.departmental"
          placeholder="请选择部门"
          :options="departmentData"
          :props="{
            label: 'departName',
            value: 'departName',
            emitPath: false,
            checkStrictly: true,
          }"
          filterable
          class="width-fill"
        />
      </el-form-item>
      <el-form-item
        label="设备名称"
        prop="deviceName"
        class="width-fill"
      >
        <el-input v-model="formData.deviceName" />
      </el-form-item>
      <el-form-item
        label="设备编号"
        prop="deviceNumber"
        class="width-fill"
      >
        <el-input v-model="formData.deviceNumber" />
      </el-form-item>
      <el-form-item
        label="设备型号规格"
        prop="deviceModel"
        class="width-fill"
      >
        <el-input v-model="formData.deviceModel" />
      </el-form-item>
      <el-form-item
        label="安装地点"
        prop="installationLocation"
        class="width-fill"
      >
        <el-input v-model="formData.installationLocation" />
      </el-form-item>
      <el-form-item
        prop="devicePower"
        label="消耗量或功率"
        class="width-fill"
      >
        <el-input v-model.number="formData.devicePower" />
      </el-form-item>
      <el-form-item
        prop="useEnergyType"
        label="用能种类"
        class="width-fill"
      >
        <el-select
          v-model="formData.useEnergyType"
          class="width-fill"
        >
          <el-option
            v-for="item in ENERGY.typeList"
            :key="item.type"
            :label="item.label"
            :value="item.type"
          />
        </el-select>
      </el-form-item>

      <el-form-item
        prop="keyTechnicalData"
        label="主要技术参数"
        class="width-fill"
      >
        <el-input
          v-model.trim="formData.keyTechnicalData"
          maxlength="20"
          placeholder="请输入主要技术参数"
        />
      </el-form-item>
      <el-form-item
        prop="loadRate"
        label="负载率(%)"
        class="width-fill"
      >
        <el-input
          v-model.trim.number="formData.loadRate"
          maxlength="20"
          placeholder="请输入负载率"
        />
      </el-form-item>
      <el-form-item
        prop="runTime"
        label="运行时间(h)"
        class="width-fill"
      >
        <el-input
          v-model.trim.number="formData.runTime"
          maxlength="20"
          placeholder="请输入运行时间"
        />
      </el-form-item>
      <el-form-item
        prop="status"
        label="设备状态"
        class="width-fill"
      >
        <el-select
          v-model="formData.status"
          placeholder="请选择状态"
          class="width-fill"
        >
          <el-option label="正常" :value="0" />
          <el-option label="故障" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item
        prop="planUpdateTime"
        label="计划更新时间"
        class="width-fill"
      >
        <el-date-picker
          v-model="formData.planUpdateTime"
          type="date"
          value-format="YYYY/MM/DD"
          placeholder="请选择时间"
        />
      </el-form-item>
      <el-form-item
        prop="project"
        label="项目"
        class="width-fill"
      >
        <el-input
          v-model.trim="formData.project"
          maxlength="20"
          placeholder="请输入项目"
        />
      </el-form-item>
      <el-form-item
        prop="index"
        label="指标"
        class="width-fill"
      >
        <el-input
          v-model.trim="formData.index"
          maxlength="20"
          placeholder="请输入指标"
        />
      </el-form-item>
      <el-form-item label="备注" class="width-fill">
        <el-input
          v-model.trim="formData.remarks"
          maxlength="20"
          :rows="2"
          type="textarea"
          placeholder="请输入备注"
        />
      </el-form-item>
    </el-form>
  </CustomDialog>
</template>

<style lang="scss" scoped>
//------------------组件样式------------------
//-------------样式穿透-------------
</style>
